package com.azzaou.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.azzaou.dao.IClientDao;
import com.azzaou.model.Chantier;
import com.azzaou.model.Client;

public class ClientDao extends HibernateDaoSupport implements IClientDao {

	@Override
	public List<Client> getClientByCriteria(Client client) {
		 List<Client> clientList = new ArrayList<Client>();		 
		 StringBuffer requete = new StringBuffer("FROM Client WHERE 1=1 ");

		  if (client != null) {
				if (!StringUtils.isBlank(client.getNom())) {
					requete.append(" AND nom = :nom ");
				}
				if (!StringUtils.isBlank(client.getPrenom())) {
					requete.append(" AND prenom= :prenom ");
				}
				if (client.getDateCreate()!=null) {
					requete.append(" AND dateCreate = :dateCreate ");
				}
				if (!StringUtils.isBlank(client.getTelephone())) {
					requete.append(" AND telephone = :telephone ");
				}
		  }
		  Query query = getSession().createQuery(requete.toString());
		  
		  if (client != null) {
			  if (!StringUtils.isBlank(client.getNom())) {
				  query.setString("nom", client.getNom());
				}
				if (!StringUtils.isBlank(client.getPrenom())) {
					query.setString("prenom", client.getPrenom());
				}
				if (client.getDateCreate()!=null) {
					query.setDate("dateCreate", client.getDateCreate());
				}
				if (!StringUtils.isBlank(client.getTelephone())) {
					query.setString("telephone", client.getTelephone());
				}
		  }
		  clientList = query.list();
		 return clientList;
	}

	@Override
	public void saveOrUpdate(Client client) {
		getHibernateTemplate().saveOrUpdate(client); 

	}

	@Override
	public void remove(Client client) {
		getHibernateTemplate().delete(client); 

	}

	@Override
	public Client getClientById(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Client> getAllClients() {
		List<Client> clientList = new ArrayList<Client>();
	    Query query = getSession().createQuery(" FROM Client");
	    clientList = query.list();
	    return clientList;
	}

}
